// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Settings.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Settings.cs
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Services.BingMaps
{
    /// <summary>
    /// Settings class used with Bing Maps service.
    /// </summary>
    public class Settings : ISettings
    {
        private const int DefaultRetryCount = 5;
        private const int DefaultTimeout = 10000;
        private const int DefaultCacheTimeout = 600000;

        private const string LocationAddressUrlTemplate = "http://dev.virtualearth.net/REST/v1/Locations?countryRegion={0}&adminDistrict={1}&locality={2}&postalCode={3}&addressLine={4}&key={5}";

        private const string LocationPointUrlTemplate = "http://dev.virtualearth.net/REST/v1/Locations/{0}?includeEntityTypes={1}&key={2}";

        private const string FindLocationQueryUrlTemplate = "http://dev.virtualearth.net/REST/v1/Locations?{0}&key={1}";

        private const string RoutesCalculateRouteUrlTemplate = "http://dev.virtualearth.net/REST/v1/Routes/{0}?{1}&heading={2}&optimize={3}&avoid={4}&timeType={5}&dateTime={6}&maxSolutions={7}&routePathOutput={8}&distanceUnit={9}&key={10}";

        private const string RoutesCalculateRoutesMajorRoadsUrlTemplate = "http://dev.virtualearth.net/REST/v1/Routes/FromMajorRoads?destination={0}&exclude={1}&rpo={2}&du={3}&key={4}";

        private const string ImageryCenterPointUrlTemplate = "http://dev.virtualearth.net/REST/v1/Imagery/Map/{0}/{1}/{2}?mapSize={3}&mapLayer={4}&mapVersion={5}{6}&key={7}";

        private const string ImageryCenterPointWithRouteUrlTemplate = "http://dev.virtualearth.net/REST/v1/Imagery/Map/{0}/{1}/{2}/Routes/{3}?{4}&mapSize={5}&avoid={6}{7}&timeType={8}&dateTime={9}&maxSolutions={10}&mapLayer={11}&mapVersion={12}&key={13}";

        private const string ImageryMapAreaUrlTemplate = "http://dev.virtualearth.net/REST/v1/Imagery/Map/{0}?mapArea={1}&mapSize={2}{3}&mapLayer={4}&mapVersion={5}&key={6}";

        private const string ImageryMapAreaWithRouteUrlTemplate = "http://dev.virtualearth.net/REST/v1/Imagery/Map/{0}/Routes/{1}?{2}&mapArea={3}&avoid={4}{5}&timeType={6}&dateTime={7}&maxSolutions={8}&mapLayer={9}&mapVersion={10}&key={11}";

        private const string ImageryMapRouteUrlTemplate = "http://dev.virtualearth.net/REST/v1/Imagery/Map/{0}/Routes/{1}?avoid={2}&dateTime={3}&mapLayer={4}&mapSize={5}&maxSolutions={6}&mapVersion={7}&optimize={8}{9}&timeType={10}{11}&key={12}";

        private const string ImageryQueryUrlTemplate = "http://dev.virtualearth.net/REST/v1/Imagery/Map/{0}/{1}?mapSize={2}&mapLayer={3}&mapVersion={4}&key={5}";

        private const string ImageryGetImageryMetadataUrlTemplate = "http://dev.virtualearth.net/REST/v1/Imagery/Metadata/{0}?key={1}";

        private const string ImageryGetImageryMetadataAtSpecificLocationUrlTemplate = "http://dev.virtualearth.net/REST/v1/Imagery/Metadata/{0}/{1}?orientation={2}&zoomLevel={3}{4}&key={5}";

        private const string ImageryGetImageryBasicMetadataAtSpecificLocationUrlTemplate = "http://dev.virtualearth.net/REST/v1/Imagery/BasicMetadata/{0}/{1}?orientation={2}&zoomLevel={3}{4}&key={5}";
        
        private const string SearchUrlTemplate = "http://api.bing.net/json.aspx?AppId={0}&Version=2.2&Market={1}&Latitude={2}&Longitude={3}&Radius={4}&Query={5}&Sources={6}&Web.Count={7}";

        /// <summary>
        /// Initialise the setting class for use with Bing Maps service.
        /// The request timeout value is set to 10 seconds.
        /// The request retry value is set to 5.
        /// The cache timeout value is set to 600 seconds.
        /// </summary>
        /// <param name="credentialsId">The Bing Maps API credentials</param>
        /// <param name="appId">The Bing Maps API application Id</param>
        public Settings(string credentialsId, string appId)
            : this(credentialsId, appId, DefaultTimeout, DefaultRetryCount, DefaultCacheTimeout)
        {
        }

        /// <summary>
        /// Initialise the setting class for use with Bing Maps service.
        /// The cache timeout value is set to 600 seconds.
        /// </summary>
        /// <param name="credentialsId">The Bing Maps API credentials</param>
        /// <param name="appId">The Bing Maps API application Id</param>
        /// <param name="timeout">The request timeout value in milliseconds.</param>
        /// <param name="retry">The request retry count</param>
        public Settings(string credentialsId, string appId, int timeout, int retry)
            : this(credentialsId, appId, timeout, retry, DefaultCacheTimeout)
        {
        }

        /// <summary>
        /// Initialise the setting class for use with Bing Maps service. 
        /// </summary>
        /// <param name="credentialsId">The Bing Maps API credentials</param>
        /// <param name="appId">The Bing Maps API application Id</param>
        /// <param name="timeout">The request timeout value in milliseconds.</param>
        /// <param name="retry">The request retry count</param>
        /// <param name="cacheTimeout">Teh cache timeout value in milliseconds</param>
        public Settings(string credentialsId, string appId, int timeout, int retry, int cacheTimeout)
        {
            this.CredentialsId = credentialsId;
            this.AppId = appId;
            this.Timeout = timeout;
            this.Retry = retry;
            this.CacheTimeout = cacheTimeout;
        }

        #region Implementation of ISettings

        public string AppId { get; private set; }

        public string CredentialsId { get; private set; }

        public int CacheTimeout { get; set; }

        public int Timeout { get; set; }

        public int Retry { get; set; }

        public string CalculateRouteUrl
        {
            get
            {
                return RoutesCalculateRouteUrlTemplate;
            }
        }

        public string CalculateRoutesFromMajorRoadsUrl
        {
            get
            {
                return RoutesCalculateRoutesMajorRoadsUrlTemplate;
            }
        }

        public string ImageryCenterPointUrl
        {
            get
            {
                return ImageryCenterPointUrlTemplate;
            }
        }

        public string ImageryCenterPointWithRouteUrl
        {
            get
            {
                return ImageryCenterPointWithRouteUrlTemplate;
            }
        }

        public string ImageryMapAreaUrl
        {
            get
            {
                return ImageryMapAreaUrlTemplate;
            }
        }

        public string ImageryMapAreaWithRouteUrl
        {
            get
            {
                return ImageryMapAreaWithRouteUrlTemplate;
            }
        }

        public string ImageryMapRouteUrl
        {
            get
            {
                return ImageryMapRouteUrlTemplate;
            }
        }

        public string ImageryQueryUrl
        {
            get
            {
                return ImageryQueryUrlTemplate;
            }
        }

        public string ImageryMetadataUrl
        {
            get
            {
                return ImageryGetImageryMetadataUrlTemplate;
            }
        }

        public string ImageryMetadataAtSpecificLocationUrl
        {
            get
            {
                return ImageryGetImageryMetadataAtSpecificLocationUrlTemplate;
            }
        }

        public string ImageryMetadataBasicAtSpecificLocationUrl
        {
            get
            {
                return ImageryGetImageryBasicMetadataAtSpecificLocationUrlTemplate;
            }
        }

        public string FindLocationUsingQueryUrl
        {
            get
            {
                return FindLocationQueryUrlTemplate;
            }
        }

        public string SearchLocationUsingAddressUrl
        {
            get
            {
                return LocationAddressUrlTemplate;
            }
        }

        public string SearchLocationUsingPointUrl
        {
            get
            {
                return LocationPointUrlTemplate;
            }
        }

        public string SearchUrl
        {
            get
            {
                return SearchUrlTemplate;
            }
        }

        #endregion
    }
}